<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>菜鸟教程(runoob.com)</title>
    <script src="../js/vue.min.js"></script>
    <script src="../js/vuex.js"></script>
</head>
<body>
<div id="app">
    <p>{{count}}
        <button @click="inc">+</button>
        <button @click="dec">-</button>
        <button @click="addList">显示数据</button>
    </p>
    {{dataList}}
    你可以向 store.commit 传入额外的参数，即 mutation 的 载荷（payload）：
    <br>
    在大多数情况下，载荷应该是一个对象，这样可以包含多个字段并且记录的 mutation 会更易读：
</div>
<script>

    const store = new Vuex.Store({
                state: {
                    count: 0,
                    list:""
                },
                mutations: {
                    inc:function(state,payload){ state.count += payload.amount},
                    dec:function(state,payload){ state.count -= payload.amount},
                    addList:function(state,payload){state.list=payload.data}
    }
    })

    const app = new Vue({
        el: '#app',
        computed: {
            count () {
                return store.state.count
            },
            dataList(){
                return store.state.list
            }
        },
        methods: {
            inc () {
                store.commit('inc',{amount: 10})
            },
            dec () {
                store.commit('dec',{amount: 10})
            },
            addList(){
                store.commit('addList',{data:[{id:"122",productname:"chinese car"},{id:"33",productname:"Japenese car"}]})
            }
        }
    })
</script>
</body>
</html>
